﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Defecter.Gui.Access;
using DefectDBTypes;
using System.IO;



namespace Defector
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        String server;
        String database;
        String username;
        String password;

        public Login()
        {
            InitializeComponent();

        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {

            login();

            

        }

        //If enter is pressed in the password field 
        private void passwordInput_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                login();
        }

        //If enter is pressed in the username field
        private void emailInput_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                login();
        }

        public void login()
        {
            try
            {
                TextReader tr = new StreamReader("Configuration.txt");
                server = tr.ReadLine();
                database = tr.ReadLine();
                username = tr.ReadLine();
                password = tr.ReadLine();
                tr.Close();
                SqlConnection con = new SqlConnection("Data Source=" + @server + ";Initial Catalog=" + database + ";User ID=" + username + ";Password=" + password);
                con.Open();
                SqlCommand cmd = new SqlCommand("Select * from People where Email= @email and Password= @password", con);
                cmd.Parameters.AddWithValue("@email", emailInput.Text);
                cmd.Parameters.AddWithValue("@password", passwordInput.Password);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet, "People");


                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    MainWindow main = new MainWindow();
                    main.Show();
                    Application.Current.MainWindow = main;
                    DataRow user = dataSet.Tables["People"].Rows[0];
                    main.userID = Convert.ToInt32(user.ItemArray.GetValue(0).ToString());
                    main.firstName = user.ItemArray.GetValue(1).ToString();
                    main.lastName = user.ItemArray.GetValue(2).ToString();
                    main.con = con;
                    this.Close();
                }
                else
                {

                    errorLabel.Content = "Error! Login not found!";

                }

                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please configure your client!");
            }
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void configureButton_Click(object sender, RoutedEventArgs e)
        {
            Configure configure = new Configure();
            configure.Show();
        }
    }
}
